import {
    LOGIN_OK,
    GET_USER_INFO,
    USER_LIST
} from '../actionType'

const initState={
    token:localStorage.getItem('token'),
    //用于控制"菜单权限""Route权限"
    userinfo:{},
    userList:[],
    total:1
}

export default function reducer(state=initState,{type,payload}){
    let newState=JSON.parse(JSON.stringify(state))
    switch (type) {
        case LOGIN_OK:
            //为什么不是action传过来的payload？因为状态管理的数据刷新或者关闭游览器后就会消失，localStorage不会
            newState.token=localStorage.getItem('token')
            break;
        case GET_USER_INFO:
            newState.userinfo=payload
            break;
        case USER_LIST:
            newState.userList = payload.list
            newState.total = payload.total
            break;
    }
    return newState
}